$(function () {
    var settings = {
        async: {
            dataType: 'json',
            dataFilter: function (treeId, treeNode, response) {
                if (response.success) {
                    var list = response.entity;
                    return list;
                } else {
                    icModale(0, response.message);
                }
            },
            enable: true,
            type: 'post',
            url: baseloaction + "/admin/manage/group/ajaxGroupConditionList" //仅查询正常状态
        },
        data: {
            key: {
                name: 'groupName',
                title: 'depict',
                status: 'status'
            },
            simpleData: {
                enable: true,
                idKey: 'id',
                pIdKey: 'parentId',
                rootPId: null
            }
        },
        callback: {
            onAsyncError: function (event, treeId, treeNode, request, status) {
                icModale(0, "系统错误，部门数据列表失败");
            },
            onAsyncSuccess: function (event, treeId, treeNode, msg) {
                var treeObj = $.fn.zTree.getZTreeObj(treeId);
                treeObj.expandAll(true);
                if ($('#group li').size() == 0) {
                    $('#group-count').html('');
                    $('#group').html('<p><span class="c-4e fs16">亲，还没有部门~~~</span></p>');
                }
            },
            onClick: function (event, treeId, treeNode) {//点击节点的时候
                var groupId = treeNode.id;
                var groupName = treeNode.groupName;
                var container = [];
                container.push(groupId);
                while ((treeNode = treeNode.getParentNode()) != null) {
                    container.push(treeNode.id);
                }
                var groupIds = container.reverse().join(',');

                var _iframe = $("#main-body-context iframe:visible",window.parent.document);
                var iframeName = contentMainIframeName();
                parent.frames[iframeName].selectGroupCallback(groupId, groupIds, groupName);
                parent.layer.closeAll();
            }
        }
    };
    //初始化权限树
    $.fn.zTree.init($("#group"), settings);
    $('body').on('keyup', '#search-group', function () { //搜索权限树
        var keyword = this.value;
        var treeObj = $.fn.zTree.getZTreeObj("group");
        if ($.isEmptyObject($.trim(keyword))) {
            var nodes = treeObj.getNodesByFilter(function (node) {
                return node.highlight;
            }, false);
            if (nodes.length > 0) {
                nodes.forEach(function (node) {
                    node.highlight = false;
                    treeObj.updateNode(node);
                });
            }
            return;
        }
        treeObj.getNodesByFilter(function (node) {
            return node.highlight;
        }, false).forEach(function (node) {
            node.highlight = false;
            treeObj.updateNode(node);
        });
        treeObj.getNodesByFilter(function (node) {
            return node.groupName.indexOf(keyword) != -1;
        }).forEach(function (node) {
            node.highlight = true;
            treeObj.updateNode(node);
        });
    });
});
